import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/request/login_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_center_alert_widget.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/user_center/widget/binding_facility_send_code_widget.dart';
import 'package:flutter_app_unionad/view_model/change_device_event.dart';

//绑定设备
class BindingFacilityWidget extends BaseCloseAlertWidget {
  String? device;

  @override
  Widget buildContent(BuildContext context) {
    return Column(
      children: [
        MySizeBox(height: 30),
        Text(
          "请输入设备号",
          style: TextStyle(
            color: MyColors.color_27_27_27,
            fontSize: MyFont.font16,
            fontWeight: MyFontWeight.bold,
          ),
        ),
        MySizeBox(height: 10),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 69.px),
          child: InputSeparatorWidget(
            valueChange: (value) {
              device = value;
            },
            title: "",
            keyboard: TextInputType.number,
            placeholder: "请输入",
            titleWidth: 0,
            inputFont: MyFont.font16,
          ),
        ),
        MySizeBox(height: 30),
        Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            MyButton(
              title: "解绑",
              radius: 1.px,
              color: MyColors.color_201_201_201,
              onTap: () {
                bindingDevice(false, context);
              },
              width: 133.px,
            ),
            MySizeBox(width: 10),
            MyButton(
              title: "绑定",
              radius: 1.px,
              onTap: () {
                bindingDevice(true, context);
              },
              width: 133.px,
            )
          ],
        )
      ],
    );
  }

  void bindingDeviceInputMobile(BuildContext context, String device, String mobile) {
    ShowCenterAlertDialog.showAlert(
      context,
      builder: (ctx) {
        return BindingFacilitySendCodeWidget(
          device,
          mobile,
        );
      },
      width: 335.px,
      height: 270.px,
    );
  }

  void bindingDevice(bool binging, BuildContext context) {
    FocusScope.of(context).requestFocus(FocusNode());
    if (device == null || device!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "请输入设备号");
      return;
    }

    ProgressDialog.showProgress(context);
    if (binging) {
      LoginRequest.bindingDeviceGetMobile(devId: device).then((value) {
        ProgressDialog.dismiss(context);
        if (value != null) {
          NavigatorUtil.pop(context);
          bindingDeviceInputMobile(context, device ?? '', value);
        }
      });
    } else {
      LoginRequest.unBindingDevice(devId: device).then((value) {
        ProgressDialog.dismiss(context);

        if (value) {
          eventBus.fire(ChangeDeviceEvent(true));
          NavigatorUtil.pop(context);
        }
      });
    }
  }
}
